package com.behome.netty.netty_A.bean;

import com.behome.netty.common.CapCmdManager;
import com.behome.netty.netty_A.enums.ProtocolA50Annotation;
import lombok.extern.slf4j.Slf4j;

import java.nio.ByteBuffer;

@Slf4j
public abstract class ProtocolAPacket extends ProtocolAPacketBase {
    protected static final byte HEAD_FLAG = 80;
    protected static final byte PLATFORM_TYPE = 2;
    private REBACK_TYPE rebackType;

    public ProtocolAPacket(byte[] data, REBACK_TYPE rebackType) {
        super(data);
        this.rebackType = rebackType;
        this.isSign = true;
    }

    @Override
    public byte[] getSignData(byte[] data) {
//        return this.isSign? CryptoFactory.getCrypto().signature(data):new byte[0];
        return new byte[0];
    }

    public String toSimpleJsonString() {
        return "";
    }

    public abstract String toCHJsonString();

    @Override
    public byte getCmdTypeValue() {
        ProtocolA50Annotation annotation = this.getClass().getAnnotation(ProtocolA50Annotation.class);
        if(annotation != null) {
            return annotation.type().getValue();
        } else {
            throw new IllegalArgumentException();
        }
    }

    @Override
    public String getCmdType() {
        ProtocolA50Annotation annotation = this.getClass().getAnnotation(ProtocolA50Annotation.class);
        if(annotation != null) {
            return annotation.type().name();
        } else {
            throw new IllegalArgumentException();
        }
    }

    @Override
    public String getCmdTypeName() {
        ProtocolA50Annotation annotation = this.getClass().getAnnotation(ProtocolA50Annotation.class);
        if(annotation != null) {
            return annotation.type().getName();
        } else {
            throw new IllegalArgumentException();
        }
    }

    @Override
    public byte getCmdHeadFlag() {
        return HEAD_FLAG;
    }

    @Override
    public byte getCmdSourceValue() {
        return PLATFORM_TYPE;
    }

    @Override
    public String getPacketInfo() {
        String ret = "未知";
        ProtocolA50Annotation annotation = this.getClass().getAnnotation(ProtocolA50Annotation.class);
        if(annotation != null) {
            ret = annotation.type().toString();
        }

        return ret;
    }

    public static ProtocolAPacket deserialize(byte[] data) {
        if(data != null && data.length > MIN_LENGTH && data[0] == HEAD_FLAG) {
            ByteBuffer byteBuffer = ByteBuffer.wrap(data);
            short headVersion = byteBuffer.getShort(1);
            int bodyLength = byteBuffer.getInt(5);
            byte[] body = new byte[bodyLength];
            byteBuffer.position(9);
            byteBuffer.get(body, 0, bodyLength);
            Byte aByte = data[3];
            Class<ProtocolAPacket> cmdClass = CapCmdManager.getProtocolAClass(aByte);
            if(cmdClass != null) {
                try {
                    ProtocolAPacket cmdObj = cmdClass.newInstance();
                    cmdObj.headVersion = headVersion;
                    cmdObj.deserializeBody(body);
                    return cmdObj;
                } catch (InstantiationException | IllegalAccessException var7) {
                    var7.printStackTrace();
                }
            }
        }

        return null;
    }

    public REBACK_TYPE getRebackType() {
        return this.rebackType;
    }

    public abstract String getSourceCode();

    public static enum REBACK_TYPE {
        主动上报,
        被动上报;
        private REBACK_TYPE() {
        }
    }
}
